package com.whjk.safework.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.db.nosql.redis.RedisDS;
import cn.hutool.json.JSONObject;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.whjk.safework.entity.ResponseData;
import com.whjk.safework.service.IWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import java.util.List;

@RestController
@RequestMapping(value = "/push")
public class Push {

    @Autowired
    private IWorkerService workerService;

    @RequestMapping(value="/index",method = RequestMethod.GET)
    public String index(){
        Jedis   jedis = RedisDS.create().getJedis();
                jedis.set("jjv5","测试");
        return "";
    }


    @RequestMapping(value="/getv",method = RequestMethod.GET)
    public ResponseData getv(@RequestParam(value = "k", defaultValue = "") String k){

        ResponseData   responseData      =  new ResponseData();
        Jedis           jedis            =  RedisDS.create().getJedis();
        responseData.setData(jedis.get(k));
        return responseData;
    }


    @RequestMapping(value="/pushtest",method = RequestMethod.GET)
    public ResponseData pushtest(){

        ResponseData   responseData     =  new ResponseData();
        Jedis          jedis            =  RedisDS.create().getJedis();
        String         token            =  jedis.get("access_token_min_program");

        JSONObject     body    =   new JSONObject();
                       body.set("touser","oUDAb5GCaVMW-9JzCKJjmDV3mHVg");
                       body.set("template_id","jR_nsI0pIao2RCSU8FdVF2_JVxHxZA7I875bAXBZJSc");

        JSONObject  json=new JSONObject();
                    json.set("thing1",new JSONObject().set("value","您的履职任务提醒"));
                    json.set("thing2",new JSONObject().set("value", "逾期任务自评提醒"));
                    json.set("time4",new JSONObject().set("value", DateUtil.now()));
                    json.set("thing5",new JSONObject().set("value","逾期37条，其中周逾期36条，月逾期1条"));
        body.set("data",json);
        //发送
        String accessToken  =  token;
        String post         =  cn.hutool.http.HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, body.toString());

        responseData.setData(post);

        return responseData;
    }

    @RequestMapping(value="/push",method = RequestMethod.GET)
    public ResponseData push(){

        ResponseData   responseData     =  new ResponseData();
        Jedis          jedis            =  RedisDS.create().getJedis();
        String         token            =  jedis.get("access_token_min_program");
        if(token==null){
            token   =  cn.hutool.http.HttpUtil.get("https://api.lvz.zhaq.zgsepri.com/Index/refresh");

            System.out.println(token);

            if(token.equals("error")){
                responseData.setMsg("token fail");
            }
            else {
                List<Row> workerList   =   workerService.getPushWorker();

                for (Row workerRow:workerList) {
                    List<Row>  numRowList  =  Db.selectListBySql("select count(*) as num,check_pc from t_worker_target where   worker_id=? and pass_time  IS NULL  and   end_time<now() and DATE_FORMAT(start_time,'%Y')= ? group by check_pc",workerRow.getString("id"),DateUtil.year(DateUtil.date()));
                    int yq_week     =   0;
                    int yq_month    =   0;
                    int yq_quarter  =   0;
                    int yq_year     =   0;

                    for (Row numrow:numRowList) {
                            if(numrow.getString("check_pc").equals("1次/每周"))    yq_week       =   numrow.getInt("num");
                            if(numrow.getString("check_pc").equals("1次/每月"))    yq_month      =   numrow.getInt("num");
                            if(numrow.getString("check_pc").equals("1次/每季度"))  yq_quarter    =   numrow.getInt("num");
                            if(numrow.getString("check_pc").equals("1次/每年"))    yq_year       =   numrow.getInt("num");
                    }
                    JSONObject       body    =   new JSONObject();
                                    body.set("touser",workerRow.getString("open_id"));
                                    body.set("template_id","jR_nsI0pIao2RCSU8FdVF2_JVxHxZA7I875bAXBZJSc");

                    String      thing5      =  "逾期"+(yq_week+yq_month+yq_quarter+yq_year)+"条";//,周"+yq_week+"条,月"+yq_month+"条,季度"+yq_quarter+"条
                    if(yq_week>0){
                        thing5 = thing5+"周"+yq_week+"条";
                    }
                    if(yq_month>0){
                        thing5 = thing5+"月"+yq_month+"条";
                    }
                    if(yq_quarter>0){
                        thing5 = thing5+"季度"+yq_quarter+"条";
                    }

                    JSONObject  json        =   new JSONObject();
                                json.set("thing1",new JSONObject().set("value","您的履职任务提醒"));
                                json.set("thing2",new JSONObject().set("value", "逾期任务自评提醒"));
                                json.set("time4",new JSONObject().set("value", DateUtil.now()));
                                json.set("thing5",new JSONObject().set("value",thing5));
                    body.set("data",json);
                    //发送
                    String accessToken  =  token;
                    String post         =  cn.hutool.http.HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, body.toString());

                    Row  in_row = new Row();
                    in_row.set("open_id",workerRow.getString("open_id"));
                    in_row.set("day",DateUtil.year(DateUtil.date()));
                    in_row.set("msg",post);
                    in_row.set("create_time",DateUtil.date());
                    in_row.set("info",thing5);
                    workerService.addPushRecord(in_row);
                }
            }
        }
        else  System.out.println("not null:"+token);
        responseData.setData("");
        return responseData;
    }


    public static void main(String[] args) {
        Jedis          jedis            =  RedisDS.create().getJedis();
        String         token            =  jedis.get("jjv5");
        if(token==null){
            System.out.println("null");
        }
        else  System.out.println("not null:"+token);
    }



}
